package com.xingkeduo.utils;

import java.util.Arrays;

public class WeekUtil {

    //日期字典
    private static String[] DATES = {"","周一","周二","周三","周四","周五","周六","周日"};

    public static void main(String[] args) {
        String s = convertWeek("1,2,4,5,7");
        System.out.println(s);
        
    }

    /**
     * 将数字转换为周数
     *
     * @author  chenxiangda
     * @date 2019年4月12日 16:41:03
     * @param numbers 字符串数字 ["1","2",...]
     * @return 字符串 "周一至周二"
     */
    public static String convertWeek(String numbers){
        String[] data = numbers.split(",");
        int[] ints = new int[data.length];
        //转换为int
        for (int i=0;i<data.length;i++) {
        //    ints[i] = StringUtil.parseInt(data[i]);
            ints[i] = Integer.valueOf(data[i]);
        }
        //1.先正序排序
        Arrays.sort(ints);
        String s = "";
        //存储开始日期
        int tempBegin = -1;
        //存储结束日期
        int tempEnd = -1;
        //判断是否连续
        for (int i=1;i<=ints.length;i++) {
            //判断是否为最后一位
            if (i == ints.length) {
                //判断是否有连续的
                if (-1 != tempBegin && -1 != tempEnd) {
                    s += DATES[tempBegin]+"至"+DATES[tempEnd]+",";
                    break;
                }
                s += DATES[ints[i-1]]+",";
                break;
            }
            // 1+1 = 2  代表连续
            if (ints[i] == ints[i-1] + 1) {
                if (-1 == tempBegin) {
                    tempBegin = ints[i-1];
                }
                tempEnd = ints[i];
            } else {
                if (-1 != tempBegin && -1!= tempEnd) {
                    s += DATES[tempBegin]+"至"+DATES[tempEnd]+",";
                    tempBegin = -1;
                    tempEnd = -1;
                    continue;
                }
                s += DATES[ints[i-1]]+",";
            }
        }
        return s.substring(0,s.length()-1);
    }
    public static Integer getDayWeek(String weeks){
    	for(int i=0;i<DATES.length;i++) {
    		if(DATES[i].equalsIgnoreCase(weeks)) {
    			return i;
    		}
    	}
    	return 0;
    }
}
